package com.mxnet.chongjin.supoman.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

import com.mxnet.chongjin.supoman.R;
import com.mxnet.chongjin.supoman.adapter.MachineInsAdapter;
import com.mxnet.chongjin.supoman.fragment.OrderAcceptedFragment;
import com.mxnet.chongjin.supoman.fragment.OrderApplyingFragment;
import com.mxnet.chongjin.supoman.fragment.OrderDoneFragment;

import java.util.ArrayList;
import java.util.List;

public class RepairOrderActivity extends AppCompatActivity implements View.OnClickListener {
    private Context mContext;
    private ViewPager mViewPager;
    private ImageView mImageView;
    private TabLayout mTabLayout;
    private MachineInsAdapter mInsAdapter;
    private FragmentManager mFragmentManager;
    private List<Fragment> mFragmentList;
    private TabLayout.Tab mTab;
    private List<String> mList;
    private OrderApplyingFragment mOrderApplyingFragment;
    private OrderAcceptedFragment mOrderAcceptedFragment;
    private OrderDoneFragment mOrderDoneFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repair_order);
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        getWindow().setStatusBarColor(Color.TRANSPARENT);
        mContext = RepairOrderActivity.this;

        initView();
        initData();
    }

    public void initData() {
        mFragmentManager = getSupportFragmentManager();
        mList = new ArrayList<>();
        mList.add(getResources().getString(R.string.repair_order_applying));
        mList.add(getResources().getString(R.string.repair_order_accepted));
        mList.add(getResources().getString(R.string.repair_order_done));

        mFragmentList = new ArrayList<>();

        mOrderApplyingFragment = new OrderApplyingFragment();
        mFragmentList.add(mOrderApplyingFragment);

        mOrderAcceptedFragment = new OrderAcceptedFragment();
        mFragmentList.add(mOrderAcceptedFragment);

        mOrderDoneFragment = new OrderDoneFragment();
        mFragmentList.add(mOrderDoneFragment);

        mInsAdapter = new MachineInsAdapter(mFragmentManager, mFragmentList);
        mViewPager.setAdapter(mInsAdapter);

        for (int i = 0; i < mFragmentList.size(); i++) {
            mTab = mTabLayout.getTabAt(i);
            mTab.setText(mList.get(i));
        }
    }

    public void initView() {
        mImageView = findViewById(R.id.iv_repair_order_add);
        mImageView.setOnClickListener(this);

        mViewPager = findViewById(R.id.vp_repair_order);

        mTabLayout = findViewById(R.id.tab_repair_order);
        mTabLayout.setupWithViewPager(mViewPager);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_repair_order_add:
                Intent intent = new Intent();
                intent.setClass(mContext, RepairRecordSelfActivity.class);
                mContext.startActivity(intent);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 800 && resultCode == RESULT_OK) {
            mInsAdapter.notifyDataSetChanged();
            mOrderApplyingFragment.onActivityResult(requestCode, resultCode, data);
            mOrderAcceptedFragment.onActivityResult(requestCode, resultCode, data);
        }
    }
}
